package abel.wlmr.config;

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class PacketCounter {

    private ReentrantReadWriteLock rw = new ReentrantReadWriteLock();

    private int sendCount;

    private int recvCount;

    private int sendFailed;

    private int recvFailed;

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return String.format("{sendCount:%s, recvCount:%s, sendFailed:%s, recvFailed:%s}", sendCount, recvCount,
            sendFailed, recvFailed);
    }

    /**
     * @return the sendCount
     */
    public int getSendCount() {
        try {
            rw.readLock().lock();
            return sendCount;
        } finally {
            rw.readLock().unlock();
        }
    }

    /**
     * @param sendCount
     *            the sendCount to set
     */
    public void setSendCount(int sendCount) {
        try {
            rw.writeLock().lock();
            this.sendCount = sendCount;
        } finally {
            rw.writeLock().unlock();
        }
    }

    public void incSendCount() {
        try {
            rw.writeLock().lock();
            ++sendCount;
        } finally {
            rw.writeLock().unlock();
        }
    }

    /**
     * @return the recvCount
     */
    public int getRecvCount() {
        try {
            rw.readLock().lock();
            return recvCount;
        } finally {
            rw.readLock().unlock();
        }
    }

    /**
     * @param recvCount
     *            the recvCount to set
     */
    public void setRecvCount(int recvCount) {
        try {
            rw.writeLock().lock();
            this.recvCount = recvCount;
        } finally {
            rw.writeLock().unlock();
        }
    }

    public void incRecvCount() {
        try {
            rw.writeLock().lock();
            ++recvCount;
        } finally {
            rw.writeLock().unlock();
        }
    }

    /**
     * @return the sendFailed
     */
    public int getSendFailed() {
        try {
            rw.readLock().lock();
            return sendFailed;
        } finally {
            rw.readLock().unlock();
        }
    }

    /**
     * @param sendFailed
     *            the sendFailed to set
     */
    public void setSendFailed(int sendFailed) {
        try {
            rw.writeLock().lock();
            this.sendFailed = sendFailed;
        } finally {
            rw.writeLock().unlock();
        }
    }

    public void incSendFailed() {
        try {
            rw.writeLock().lock();
            ++sendFailed;
        } finally {
            rw.writeLock().unlock();
        }
    }

    /**
     * @return the recvFailed
     */
    public int getRecvFailed() {
        try {
            rw.readLock().lock();
            return recvFailed;
        } finally {
            rw.readLock().unlock();
        }
    }

    /**
     * @param recvFailed
     *            the recvFailed to set
     */
    public void setRecvFailed(int recvFailed) {
        try {
            rw.writeLock().lock();
            this.recvFailed = recvFailed;
        } finally {
            rw.writeLock().unlock();
        }
    }

    public void incRecvFailed() {
        try {
            rw.writeLock().lock();
            ++recvFailed;
        } finally {
            rw.writeLock().unlock();
        }
    }

}
